package com.gewu.teday.fragmnet

import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.gewu.teday.R
import com.gewu.teday.bean.DataBeans
import com.gewu.teday.utils.GwDialog
import com.gewu.teday.utils.SpUtils
import com.gewu.teday.utils.Util
import java.util.*

abstract class BaseFragment() : Fragment() {

    protected var decodeParcelable: DataBeans? = null
    protected var hander: Handler? = null
    private var inflate: View? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {
        return LayoutInflater.from(context).inflate(getLayoutId(), container, false);
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        if (isAdded){
            initView()
            if (!isNeedLoad) {
                initData()
            }
        }
    }

    abstract fun getLayoutId(): Int

    abstract fun initView()

    abstract fun initData()

    //是否需要懒加载
    private val isNeedLoad = true

    //是否加载过数据
    private var isHasLoad = false

    /**
     * 从后台进入就需要加载数据
     * 并且把懒加载打开
     */
    override fun onResume() {
        super.onResume()
        if (isNeedLoad && !isHasLoad) {
            isHasLoad = true
            hander = Handler();
            decodeParcelable = SpUtils.instances?.decodeParcelable("data", DataBeans::class.java)
            initData()
            getOtherDay()
        }
    }

    private fun getOtherDay() {
        //判断日期和金额的bean是否为null
        if (decodeParcelable != null) {
            //获取第一个集合第一个日期 距离今天的天数 并且把所有日期返回  集合
            val splitDateList = Util.splitDateList(decodeParcelable?.mothlist?.get(0))
            //判断是否含有两个日期之间的集合  主要是判断是否添加过
            if (decodeParcelable?.mothlist?.containsAll(splitDateList) != true) {
                //获取原来的日期集合
                val mothlist = decodeParcelable?.mothlist;
                //获取原来每个日期所对于的金额的集合
                val moneylist = decodeParcelable?.moneylist
                //两个日期之间的所有金额的和 + 原金额
                var money:Double = 0.0;
                for (i in 0 until splitDateList.size) {
                    //因为要从 原来的集合的开头依次拿数据 所以获取当前的拿取下标 没有存取的状态下返回的是0
                    val decodeInt: Int? = SpUtils.instances?.decodeInt("indexday")
                    if (decodeInt == 0) {
                        //拿取集合最后一条数据 并且和金额相加
                        val fl = moneylist?.last()
                        money += fl!!.toDouble()
                        //最后把拿去的数据放在第一个位置
                        moneylist.add(0, fl)
                        //保存当前拿去
                    } else {
                        val fl = moneylist?.get(moneylist.size - decodeInt!! - 1)
                        money += fl!!.toDouble()
                        moneylist.add(0, fl)
                    }
                    if (decodeInt != null) {
                        SpUtils.instances?.encode("indexday", decodeInt + 1)
                    }
                }
                if (mothlist != null) {
                    mothlist.addAll(0, splitDateList.reversed())
                    val dataBeans = decodeParcelable?.let {
                        DataBeans(
                            it.videoNum,
                            (it.money.toFloat() + money).toString(),
                            it.type,
                            it.leijiNum,
                            moneylist!!,
                            mothlist,
                            it.maxMoney,
                            it.minMoney
                        )
                    }
                    SpUtils.instances?.encodeParcelable("data", dataBeans)
                }
            }


        }else{
            Log.e("TAG", "getOtherDay: +null" )
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (decodeParcelable != null) {
            decodeParcelable = null
        }
        if (hander != null) {
            hander = null
        }
    }

}